
/**
 * 页面加载事件：
 *    从JsonServer中读取用户信息
 */
function initData(){
    //创建ajax对象
    var req = new XMLHttpRequest();
    //封装请求
    req.open("get","http://localhost:3000/userList",true);
    //设置回调函数
    req.onreadystatechange=function(){
        if(req.readyState==4 && req.status==200){
            //存储到浏览器中
            sessionStorage.setItem("userList",req.responseText); 
        }
    }
    //发送请求
    req.send(null);
}   

//标识登录成功还是失败
var flag = false;
/**
 * 登录表单验证
 */
function checkForm(){
    //获取表单元素
    var username = document.querySelector("[name='username']").value;
    var password = document.querySelector("[name='password']").value;
    //非空校验
    if(!username || !password){
        //添加提示
        addDiv("用户名和密码不能为空！");
    }else{
        //登录验证
        var json = JSON.parse(sessionStorage.getItem("userList"));
        for(var i=0; i<json.length; i++){
            if(json[i].yhbm == username && json[i].mm == password){
                //添加提示
                addDiv("登录成功！");
                //存储登录名
                sessionStorage.setItem("loginName",json[i].yhxm);
                //存储供应商数据
                loadGYS();
                //存储账单数据
                loadZDXX();
                //角色信息数据
                loadRole();
                //权限信息数据
                loadPermission();
                flag = true;
                break;
            }
        }
        if(i == json.length){
            //添加提示
            addDiv("登录失败！");
            flag = false;
        }
    }

    return false;
}

//加载供应商信息
function loadGYS(){
    //创建ajax对象
    var req = new XMLHttpRequest();
    //封装请求
    req.open("get","http://localhost:3000/providerList",true);
    //设置回调函数
    req.onreadystatechange=function(){
        if(req.readyState==4 && req.status==200){
            //存储到浏览器中
            sessionStorage.setItem("providerList",req.responseText);
        }
    }
    //发送请求
    req.send(null);
}

//加载账单信息
function loadZDXX(){
    //创建ajax对象
    var req = new XMLHttpRequest();
    //封装请求
    req.open("get","http://localhost:3000/billList",true);
    //设置回调函数
    req.onreadystatechange=function(){
        if(req.readyState==4 && req.status==200){
            //存储到浏览器中
            sessionStorage.setItem("billList",req.responseText);
        }
    }
    //发送请求
    req.send(null);
}

//加载角色信息
function loadRole(){
    //创建ajax对象
    var req = new XMLHttpRequest();
    //封装请求
    req.open("get","http://localhost:3000/userRoleList",true);
    //设置回调函数
    req.onreadystatechange=function(){
        if(req.readyState==4 && req.status==200){
            //存储到浏览器中
            sessionStorage.setItem("userRoleList",req.responseText);
        }
    }
    //发送请求
    req.send(null);
}

//加载权限信息
function loadPermission(){
    //创建ajax对象
    var req = new XMLHttpRequest();
    //封装请求
    req.open("get","http://localhost:3000/permissionList",true);
    //设置回调函数
    req.onreadystatechange=function(){
        if(req.readyState==4 && req.status==200){
            //存储到浏览器中
            sessionStorage.setItem("permissionList",req.responseText);
        }
    }
    //发送请求
    req.send(null);
}

//添加遮罩层提示
function addDiv(sm){
    var zhezhao = document.createElement("div");
    zhezhao.setAttribute("id","zhezhao");
    zhezhao.style.height="100%";
    zhezhao.style.width="100%";
    zhezhao.style.position="fixed";
    zhezhao.style.left="0px";
    zhezhao.style.top="0px";
    zhezhao.style.backgroundColor="black";
    zhezhao.style.opacity="0.5";

    var ts = document.createElement("div");
    ts.setAttribute("id","ts");
    ts.innerText=sm;
    ts.style.border="1px #00E4FF solid";
    ts.style.height="50px";
    ts.style.width="200px";
    ts.style.backgroundColor="#0070A4";
    ts.style.color="white";
    ts.style.textAlign="center";
    ts.style.lineHeight="50px";
    ts.style.position="fixed";
    ts.style.left="50%";
    ts.style.top="46%";
    ts.style.transform="translate(-50%,-50%)";
    ts.style.zIndex="1";
    document.body.appendChild(zhezhao);
    document.body.appendChild(ts);
    //清除提示
    clearTS();
}

//移除提示相关div
function removeDiv(sm){
    document.getElementById("zhezhao").remove();
    document.getElementById("ts").remove();
    if(flag){
        location.href="welcome.html";
    }
}

//延迟1s移除的延迟器
function clearTS(){
    window.setTimeout("removeDiv()",1000);
} 
